var express = require('express');
const { default: mongoose } = require('mongoose');
var router = express.Router();
const {ApplyModel} = require('../model/ljf_model')
router.get('/apply', async (req, res) => {
	let applyes = await ApplyModel.find({status:'审批中'}).exec()
	let dealts = await ApplyModel.find({status:'审批完成'}).exec()
	let apply = await ApplyModel.find().exec()
	let dealt = await ApplyModel.find({done:'待办'}).exec()
	let done = await ApplyModel.find({done:'已办'}).exec()
	let leave = await ApplyModel.find({dtype:'请假'}).exec()
	let cancellation = await ApplyModel.find({dtype:'销假'}).exec()
	let applycancellation = await ApplyModel.find({aname:'张研',dtype:'请假'}).exec()
	let attendance = await ApplyModel.find({dtype:'考勤'}).exec()
	let overtime = await ApplyModel.find({dtype:'加班'}).exec()
	let evection = await ApplyModel.find({dtype:'出差'}).exec()
	res.send({applyes,dealts,apply,dealt,done,leave,cancellation,attendance,overtime,evection,applycancellation})
})
// 我的申请提交
router.post('/applySuccess', async (req, res) => {
	let _id = req.body._id
	await ApplyModel.updateOne({_id},{done:'待办',status:'审批中'})
	res.send({code:200})
})
// 我的待办委托
router.post('/dealt', async (req, res) => {
	let _id = req.body._id
	let daname = req.body.daname
	await ApplyModel.updateOne({_id},{daname})
	res.send({code:200})
})
// 我的提交审批
router.post('/applyDealt', async (req, res) => {
	let _id = req.body._id
	const [data] = await ApplyModel.find({_id}).exec()
	await ApplyModel.updateOne({_id},{done:'已办',status:'审批通过'})
	res.send({code:200,data})
})
// 我的撤销审批
router.post('/delDealt', async (req, res) => {
	let _id = req.body._id
	const [data] = await ApplyModel.find({_id}).exec()
	await ApplyModel.updateOne({_id},{done:'待办',status:'审批中'})
	res.send({code:200,data})
})
// 添加请假申请
router.post('/applyLeave', async (req, res) => {
	let body = req.body
	await ApplyModel.create(body)
	res.send({code:200})
})
//查看修改请假申请数据
router.get('/updateLeave/list',async(req,res)=>{
	let id = req.query.id
	console.log(id,'修改数据id');
	let data = await ApplyModel.find({_id:id})
	console.log(data);
	res.send({
		code:200,
		data
	})
})
//修改请假数据
router.post('/updateLeave',async(req,res)=>{
	let id = req.query.id
	let body = req.body
	console.log(id,'修改数据id');
	await ApplyModel.updateOne({_id:id},body)
	
	res.send({
		code:200,

	})
})
//删除请假数据
router.get('/delLeave',async (req,res)=>{
	let id = req.query.id
	await ApplyModel.deleteOne({_id:id})
	res.send({
		code:200
	})
})
// 添加销假申请
router.post('/cancellationLeave', async (req, res) => {
	let body = req.body
	await ApplyModel.create(body)
	res.send({code:200})
})
//查看修改销假申请数据
router.get('/updateationLeave/list',async(req,res)=>{
	let id = req.query.id
	console.log(id,'修改数据id');
	let data = await ApplyModel.find({_id:id})
	console.log(data);
	res.send({
		code:200,
		data
	})
})
//修改销假申请
router.post('/updateationLeave',async(req,res)=>{
	let id = req.query.id
	let body = req.body
	console.log(id,'修改数据id');
	await ApplyModel.updateOne({_id:id},body)
	res.send({
		code:200,
	})
})
//删除销假数据
router.get('/delationLeave',async (req,res)=>{
	let id = req.query.id
	await ApplyModel.deleteOne({_id:id})
	res.send({
		code:200
	})
})
// 添加异常申请
router.post('/attendanceLeave', async (req, res) => {
	let body = req.body
	await ApplyModel.create(body)
	res.send({code:200})
})
//查看修改异常信息
router.get('/updateAttendance/list',async(req,res)=>{
	let id = req.query.id
	console.log(id,'修改数据id');
	let data = await ApplyModel.find({_id:id})
	console.log(data);
	res.send({
		code:200,
		data
	})
})
//修改异常信息

// 添加加班申请
router.post('/overtimeLeave', async (req, res) => {
	let body = req.body
	await ApplyModel.create(body)
	res.send({code:200})
})
// 添加出差申请
router.post('/evectionLeave', async (req, res) => {
	let body = req.body
	await ApplyModel.create(body)
	res.send({code:200})
})
// 某个人所有流程单号
router.post('/applyleaveget', async (req, res) => {
	let _id = req.body._id
	let item = await ApplyModel.find({_id}).exec()
	res.send({code:200,item})
})
// 单个流程单号
router.post('/applyCancellation', async (req, res) => {
	let _id = req.body._id
	let item = await ApplyModel.find({anumber:_id,dtype:'请假'}).exec()
	res.send({code:200,item})
})
module.exports = router;